/* eslint-disable */
const Service = require('egg').Service;

class AdminService extends Service {
    async login(username, password, role,id) {
        const { ctx, app } = this;
        const admin = await ctx.model.Admin.findOne({ where: { username } });

        if (admin) {
            const hash = admin.password;
            const result = await ctx.compare(password, hash);
            if (result) {
                const token = app.jwt.sign({ username, role,id }, app.config.jwt.secret);
                return { success: true, data: { username, role: admin.role, id,token } };
            } else {
                return { success: false, message: '账号或密码不正确' };
            }
        } else {
            return { success: false, message: '该管理员不存在' };
        }
    }
    async checkAdminNameValid(username) {
        const { ctx } = this;
        const result = await ctx.model.Admin.findOne({ where: { username } });
        return result == null;
    }

    async add(admin) {
        const { ctx } = this;
        admin.password = await ctx.genHash(admin.password);
        return await ctx.model.Admin.create(admin);
    }
}

module.exports = AdminService;
